<template>
  <div>
    <!-- 新增修改表单 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="DialogFormVisibleCom" :close-on-click-modal="false"
               :close-on-press-escape="false" append-to-body>
      <el-form ref="dataForm" :model="formSubmitDataCom" :rules="rules" label-position="right" label-width="150px"
               style="width: 90%; margin:10px auto;">

        <el-row :gutter="20">
          <el-col :span="12">
            <!-- 限制条件编号 -->
            <el-form-item label="限制条件编号：" prop="restrictions_code">
              <el-input v-model="formSubmitDataCom.restrictions_code" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 期限点名称 -->
            <el-form-item label="限制条件名称：" prop="restrictions_name">
              <el-input v-model="formSubmitDataCom.restrictions_name"/>
            </el-form-item>
          </el-col>
          <!-- 限制条件 -->
          <el-col :span="12">
            <el-form-item label="限制条件" prop="restrictions_type">
              <el-select v-model="formSubmitDataCom.restrictions_type" class="filter-item" placeholder="Please select"
                         @change="selectLimitingConditions" clearable>
                <el-option v-for="item in limitingConditions" :key="item.code" :label="item.name" :value="item.code"/>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 限制条件 -->
          <el-col :span="12">
            <el-form-item label="运算符" prop="operator">
              <el-select v-model="formSubmitDataCom.operator" class="filter-item" placeholder="Please select"
                         clearable>
                <el-option v-for="item in operator" :key="item.code" :label="item.name" :value="item.code"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 常量 -->
            <el-form-item label="常量：" prop="constant">
              <el-input v-model="formSubmitDataCom.constant"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="附件条件">
              <el-select v-model="formSubmitDataCom.attachment_conditions" class="filter-item"
                         placeholder="Please select" :disabled="formSubmitDataCom.restrictions_type == 1" clearable>
                <el-option v-for="item in attachmentConditions" :key="item.code" :label="item.name"
                           :value="item.code"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 兜底权重/常量值 -->
            <el-form-item label="兜底权重/常量值：" prop="ctb_weight">
              <el-input v-model="formSubmitDataCom.ctb_weight"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="兜底条件">
              <el-select v-model="formSubmitDataCom.ctb_condition" class="filter-item" placeholder="Please select"
                         clearable>
                <el-option v-for="item in additionalPocketBottomConditions" :key="item.code" :label="item.name"
                           :value="item.code"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 兜底权重/常量值 -->
            <el-form-item label="优先级：" prop="priority">
              <el-input v-model="formSubmitDataCom.priority" :disabled="dialogStatus == 'update'"/>
            </el-form-item>
          </el-col>
          <!-- 备注 -->
          <!-- <el-col :span="24">
            <el-form-item label="备注：" prop="remark">
              <el-input v-model="formSubmitDataCom.remark" placeholder="Please select" type="textarea"></el-input>
            </el-form-item>
          </el-col> -->
        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button @click="resetFormData">
          重置
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// import PageHeaderLayout from '@/layouts/PageHeaderLayout'
// import ApeDrawer from '@/components/ApeDrawer'
// import { mapGetters } from 'vuex'
import {findoperatorlimiting, findrestrictionsType, findconditions} from '../service/baseApi'
import resetFormSubmitData from '@/views/mixins/resetFormSubmitData'

export default {
  name: 'submitForm',
  mixins: [resetFormSubmitData],
  components: {},

  props: {
    dialogFormVisibleLimit: {
      type: Boolean,
      default: false
    },
    dialogStatus: {
      type: String,
    },
    formSubmitData: {
      type: Object,
      default: function () {
        return {
          id: undefined,
          restrictions_code: "",
          restrictions_name: "",
          restrictions_type: "",
          operator: "",
          constant: "",
          attachment_conditions: "",
          attachment_conditionsname: "",
          ctb_weight: "",
          ctb_condition: "",
          priority: "",
          remark: ""
        }
      }
    },
  },
  // 定义属性
  data() {
    return {
      textMap: {
        update: '修改',
        create: '新增'
      },
      rules: {
        restrictions_code: [{required: true, message: '限制条件编号必填', trigger: 'change'}],
        restrictions_name: [{required: true, message: '限制条件名称必填', trigger: 'change'}],
        restrictions_type: [{required: true, message: '限制条件必填', trigger: 'change'}],
        operator: [{required: true, message: '运算符必填', trigger: 'change'}],
        constant: [{required: true, message: '常量必填', trigger: 'change'}],
        // attachment_conditions: [{required: true, message: '附件条件必填', trigger: 'change'}],
        ctb_weight: [{required: true, message: '兜底权重/常量值必填', trigger: 'change'}],
        // ctb_condition: [{required: true, message: '兜底条件必填', trigger: 'change'}],
        priority: [{required: true, message: '优先级必填', trigger: 'change'}],
      },
      operator: [],//运算符
      limitingConditions: [],//限制条件
      attachmentConditions: [],//附件条件
      additionalPocketBottomConditions: [],//兜底条件
    }
  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {
    DialogFormVisibleCom: {
      get() {
        return this.dialogFormVisibleLimit
      },
      set(val) {
        this.$emit('update:dialogFormVisibleLimit', val)
      }
    },
    formSubmitDataCom: {
      get() {
        return this.formSubmitData
      },
      set(val) {
        this.$emit('update:formSubmitData', val)
      }
    },
  },
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    resetFormData() {
      let exclude = []
      if (this.dialogStatus === 'create') {
        exclude = [
          'restrictions_code',
        ]
      }
      if (this.dialogStatus === 'update') {
        exclude = [
          'id',
          'restrictions_code'
        ]
      }
      this.$emit('resetFormDataEmit', {exclude})
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$emit('handleFromCreateDataEmit')
        }
      })
    },

    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.resetFormSubmitData({
            replaceObj: {
              attachment_conditionsname: ""
            }
          })
          this.$emit('handleFromUpdateDataEmit')

        }
      })
    },
    // 获取运算符、限制条件下拉框、 附加兜底条件下拉框
    getSelectionData() {
      findoperatorlimiting().then(({data}) => {
        this.operator = data.rows
      })
      findrestrictionsType().then(({data}) => {
        this.limitingConditions = data.rows
      })
      findconditions().then(({data}) => {
        this.additionalPocketBottomConditions = data.rows
      })
    },
    selectLimitingConditions(e) {
      this.resetFormSubmitData({
        replaceObj: {
          attachment_conditions: "",
        }
      })
      var dataList = []
      if (e == 3) {
        this.additionalPocketBottomConditions.map(item => {
          if (item.code == 1) {
            dataList.push(item)
          }
        })
      } else if (e == 2) {
        this.additionalPocketBottomConditions.map(item => {
          if (item.code == 2) {
            dataList.push(item)
          }
        })
      }
      this.attachmentConditions = dataList
    },

  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getSelectionData()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() {
  }, // 生命周期 - 创建之前
  beforeMount() {
  }, // 生命周期 - 挂载之前
  beforeUpdate() {
  }, // 生命周期 - 更新之前
  updated() {
  }, // 生命周期 - 更新之后
  beforeDestroy() {
  }, // 生命周期 - 销毁之前
  destroyed() {
  }, // 生命周期 - 销毁完成
  activated() {
  }, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>

</style>
